﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Filters;

namespace Web.Controllers.MVC
{
    [MvcAuthentication]
    public class ReportController : Controller
    {
        public ActionResult ShouFaChunReport()
        {
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in(" + IDs + ")").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;
            return View();
        }
        [HttpPost]
        public string ShouFaChunReportAsync(string BeginDate, string EndDate,int BillNameID)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@BeginDate",SqlDbType.DateTime),
                                            new SqlParameter("@EndDate",SqlDbType.DateTime),
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
            };
            parameters[0].Value = BeginDate;
            parameters[1].Value = EndDate;
            parameters[2].Value = BillNameID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryGoodsReport", parameters, "aaa", 3000);
            result.count = 0;
            result.data = ds.Tables[1];
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult ShouFaChunReport2()
        {
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in(" + IDs + ")").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;
            return View();
        }
        public ActionResult ChaiLiaoShunHaoReport()
        {
            return View();
        }
        [HttpPost]
        public string ChaiLiaoShunHaoReportAsync(string BeginDate, string EndDate)
        {
            GridResult result = new GridResult();
//            string sql = @"
//declare @BeginDate datetime='" + BeginDate + @"'
//declare @EndDate datetime='" + EndDate + @"'
//select @EndDate=dateadd(Day,1,@EndDate)

//select Goods.Code as GoodsCode,Goods.Name as GoodsName
//		,sum(TaskReportDetails.Qty*TaskBillProcedureDetails.Salary) as Salary
//		,sum(TaskReportDetails.Qty*TaskBillProcedureDetails.TaskTime) as TaskTime
//		,sum(TaskReportDetails.OpenTaskTime) as OpenTaskTime
//		--,sum(dbo.Fn_Value_SalaryRate(TaskAllot.AllotUserID,@ReportDate)*TaskReportDetails.Qty*TaskBillProcedureDetails.Salary) as MeritPay
//		--,TaskAllot.AllotDate
//from TaskReportDetails inner join TaskAllot on TaskReportDetails.TaskAllotID=TaskAllot.ID
//		inner join TaskBillProcedureDetails on TaskAllot.TaskBillProcedureDetailsID=TaskBillProcedureDetails.ID
//		inner join Users on TaskAllot.AllotUserID=Users.ID
//		inner join TaskBillDetails on TaskBillProcedureDetails.TaskBillDetailsID=TaskBillDetails.ID
//		inner join Goods on TaskBillDetails.GoodsID=Goods.ID
//		inner join TaskBill on TaskBillDetails.TaskBillID=TaskBill.ID
//where TaskAllot.AllotDate>=@BeginDate and TaskAllot.AllotDate<@EndDate
//group by Goods.Code,Goods.Name";

//            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(sql).Tables[0];
            result.count = 0;
            result.data = new DataTable();
            result.totalRow = new Dictionary<string, string>();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }

    }
}